from datetime import date, timedelta
from rest_framework.response import Response
from rest_framework.views import APIView
from users.models import User


class UserTotalCountView(APIView):
    def get(self, request):
        count = User.objects.count()
        return Response ({'count': count})


class UserDayIncrementView(APIView):
    def get(self, request):
        today = date.today()
        count = User.objects.filter(date_joined__gte=today).count()
        return Response({'count': count})


class UserDayActiveView(APIView):
    def get(self, request):
        today = date.today()
        count = User.objects.filter(last_login__gte=today).count()
        return Response({'count': count})


class UserDayOrdersView(APIView):
    def get(self, request):
        today = date.today()
        count = User.objects.filter(orders__create_time__gte=today).count()
        return Response({'count': count})


class UserMonthIncrementView(APIView):
    def get(self, request):
        today = date.today()

        #获取1个月前的日期
        old_date = today - timedelta(days=29)

        # 创建列表获取每日活跃人数
        date_list= []
        for i in range(0,30):
            day = old_date + timedelta(days=i)

            next_day = old_date + timedelta(days=i+1)

            count = User.objects.filter(date_joined__gte=day, date_joined__lt=next_day).count()

            date_dict = {
                "count": count,
                "date": day
            }
            date_list.append(date_dict)

        return Response(date_list)


# class UserGoodsView(GenericAPIView):
#     serializer_class = serializers.UserGoodsSerializer
#     #
#     # def get(self, request):
#     #     today = date.today()
#     #
#     #     goods = GoodVisitCount.objects.filter(date=today)
#     #     serializer = self.get_serializer(instance=goods, many=True)
#     #
#     #     return Response(serializer.data)
#     def get_queryset(self):
#         # 1,获取当天日期
#         today = date.today()
#
#         #2, 返回分类访问数量
#         return GoodVisitCount.objects.filter(date=today)
#
#     def get(self,request):
#         #1,查询分类访问数量
#         good_visits = self.get_queryset()
#
#         #2,获取序列化器
#         serializer = self.get_serializer(instance=good_visits,many=True)
#
#         #3,返回响应
#         return Response(serializer.data)